﻿
$("#sendButton").hide();
"use strict";
var connection = new signalR.HubConnectionBuilder().withUrl("/MyHub").build();  //连接服务Hub
connection.start().then(function () {
    $("#sendButton").show();
}).catch(function (ex) {
    return console.error(ex);
});
connection.serverTimeoutInMilliseconds = 30 * 60 * 1000; //设置超时时间(30分钟超时)

//发送消息
$("#sendButton").click(function () {
    var user = $("#userInput").val();
    var messge = $("#messageInput").text();
    $("#messageInput").text('');
    connection.invoke("SendMsg", user, messge) //调用服务端的方法
        .catch(function (ex) {
            return console.error(ex);
        })
});

//接收消息
connection.on("ReceivePublicMessage", function (userName, message) {  
    $("#content").append(`<p> ${userName} -接收的内容：${message} </p></br>`);
    $("#content").animate({scrollTop:100000});
});